#!/bin/sh

setup_helm() {
  echo "Helm version $(helm version --short)"

  HELM_VERSION_FULL="$(helm version --short)"
  HELM_VERSION=$(echo "${HELM_VERSION_FULL#v}" | cut -d'-' -f1 | cut -d'+' -f1)
  HELM_VERSION_MAJOR=$(echo "$HELM_VERSION" | cut -d'.' -f1)
  HELM_VERSION_MINOR=$(echo "$HELM_VERSION" | cut -d'.' -f2)
  HELM_VERSION_PATCH=$(echo "$HELM_VERSION" | cut -d'.' -f3)

  if [ "$HELM_VERSION_MAJOR" -eq 3 ] && [ "$HELM_VERSION_MINOR" -ge 9 ]; then
    echo "Helm version is at least 3.9+ ($HELM_VERSION_MAJOR.$HELM_VERSION_MINOR.$HELM_VERSION_PATCH)"
  else
    fail "Helm 3.9+ is required"
    exit 1
  fi

  for CHART_PATH in "$OPERATOR_CHART_PATH" "$CLUSTER_CHART_PATH"
  do
    if [ "$(helm dep list "$CHART_PATH" | tail -n +2 | head -n -1 | grep -v 'ok\s*$' | wc -l)" -gt 0 ]
    then
      helm dependency update --skip-refresh "$CHART_PATH"
    fi
  done
}
